<?php
/**
 * 我的面板
 */

class Panel_ManageController extends Cms_AdminController {
	
	/**
	 * 初始化
	 *
	 */
	public function init() {
		parent::init ();
	}
	
	/**
	 * 修改个人信息
	 */
	public function editinfoAction() {
		
		$this->view->setScriptFile ( 'formvalidator.js' );
		$this->view->setScriptFile ( 'formvalidatorregex.js' );
		
		$dosubmit = $this->_request->getPost ( 'dosubmit' );
		
		if (isset ( $dosubmit )) {
			
			$info = $this->_request->getPost ( 'info' );
			
			$admin = new DB_Admin ();
			$data = array ('realname' => $info ['realname'], 'email' => $info ['email'], 'lang' => $info ['lang'] );
			$return = $admin->updateByPrimary ( $data, $this->AdminSessionInfo ['uid'] );
			if ($return) {
				$this->view->perRedirect ( '操作成功！', $_SERVER ['HTTP_REFERER'] );
			} else {
				$this->view->perRedirect ( '操作失败！', $_SERVER ['HTTP_REFERER'] );
			}
		} else {
			
			$admin = new DB_Admin ();
			$admin_info = $admin->loadInfoByPrimary ( $this->AdminSessionInfo ['uid'] );
			
			$lang_dirs = glob ( DATA_PATH . 'languages/*' );
			$dir_array = array ();
			if (is_array ( $lang_dirs )) {
				foreach ( $lang_dirs as $dirs ) {
					$dir_array [] = str_replace ( DATA_PATH . 'languages/', '', $dirs );
				}
			}
			
			$this->view->assign ( 'admin_info', $admin_info );
			$this->view->assign ( 'dir_array', $dir_array );
		}
		
		$this->render ( 'Panel/manage/editinfo', null, true );
	}
	
	/**
	 * 修改密码
	 */
	public function editpwdAction() {
		
		$this->view->setScriptFile ( 'formvalidator.js' );
		$this->view->setScriptFile ( 'formvalidatorregex.js' );
		
		$dosubmit = $this->_request->getPost ( 'dosubmit' );
		
		if (isset ( $dosubmit )) {
			$old_password = $this->_request->getPost ( 'old_password' );
			$new_password = $this->_request->getPost ( 'new_password' );
			$new_pwdconfirm = $this->_request->getPost ( 'new_pwdconfirm' );
			
			$admin = new DB_Admin ();
			$admin_info = $admin->loadInfoByPrimary ( $this->AdminSessionInfo ['uid'] );
			
			if (strtolower ( md5 ( $old_password ) ) != strtolower ( $admin_info [password] )) {
				$this->view->perRedirect ( '旧密码输入错误', $_SERVER ['HTTP_REFERER'] );
			}
			
			if (empty ( $new_password ) || empty ( $new_pwdconfirm )) {
				$this->view->perRedirect ( '操作失败！', $_SERVER ['HTTP_REFERER'] );
			}
			
			$data = array ('password' => md5 ( $new_password ) );
			$return = $admin->updateByPrimary ( $data, $this->AdminSessionInfo ['uid'] );
			if ($return) {
				$this->view->perRedirect ( '操作成功！', $_SERVER ['HTTP_REFERER'] );
			} else {
				$this->view->perRedirect ( '操作失败！', $_SERVER ['HTTP_REFERER'] );
			}
		} else {
			$admin = new DB_Admin ();
			$admin_info = $admin->loadInfoByPrimary ( $this->AdminSessionInfo ['uid'] );
			
			$this->view->assign ( 'admin_info', $admin_info );
		}
		
		$this->render ( 'Panel/manage/editpwd', null, true );
	}
}
?>